package com.jungle.mvvm.sample.ui.register;

import android.annotation.SuppressLint;
import android.app.Application;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;

import com.jungle.mvvm.base.BaseViewModel;
import com.jungle.mvvm.binding.command.BindingCommand;
import com.jungle.mvvm.bus.event.SingleLiveEvent;
import com.jungle.mvvm.sample.app.WanObserver;
import com.jungle.mvvm.sample.bean.SameBean;
import com.jungle.mvvm.sample.bean.UserBean;
import com.jungle.mvvm.sample.http.WanNetwork;
import com.jungle.mvvm.sample.ui.login.LoginActivity;
import com.jungle.mvvm.sample.ui.login.LoginModel;
import com.jungle.mvvm.utils.KLog;
import com.jungle.mvvm.utils.SPUtils;
import com.jungle.mvvm.utils.ToastUtils;

import java.util.Objects;


/**
 * @ClassName RegisterViewModel
 * @Description 注册页ViewModel
 * @Author Daniel
 * @Version V1.0.0
 */
public class RegisterViewModel extends BaseViewModel {

    public ObservableField<String> userName = new ObservableField<>();
    public ObservableField<String> password = new ObservableField<>();
    public ObservableField<String> repassword = new ObservableField<>();
    public ObservableInt clearBtnVisibility = new ObservableInt();

    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();

        public SingleLiveEvent<Boolean> rpSwitchEvent = new SingleLiveEvent<>();
    }

    public RegisterViewModel(@NonNull Application application) {
        super(application);
    }

    public BindingCommand clearUserNameOnClickCommand = new BindingCommand(() -> userName.set(""));

    public BindingCommand passwordShowSwitchOnClickCommand = new BindingCommand(() -> uc.pSwitchEvent.setValue(uc.pSwitchEvent.getValue() == null || !uc.pSwitchEvent.getValue()));

    public BindingCommand repasswordShowSwitchOnClickCommand = new BindingCommand(() -> uc.rpSwitchEvent.setValue(uc.rpSwitchEvent.getValue() == null || !uc.rpSwitchEvent.getValue()));


    public BindingCommand<Boolean> onFocusChangeCommand = new BindingCommand<>(hasFocus -> {
        if (hasFocus) {
            clearBtnVisibility.set(View.VISIBLE);
        } else {
            clearBtnVisibility.set(View.INVISIBLE);
        }
    });

    public BindingCommand loginOnClickCommand = new BindingCommand(() -> login());

    public BindingCommand registerOnClickCommand = new BindingCommand(() -> register());

    @SuppressLint("CheckResult")
    private void register() {
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort("请输入密码！");
            return;
        }
        if (TextUtils.isEmpty(repassword.get())) {
            ToastUtils.showShort("请确认密码！");
            return;
        }
        if (!password.get().equals(repassword.get())) {
            ToastUtils.showShort("请确认两次输入密码一致！");
            return;
        }

        LoginModel
                .register(userName.get(), password.get(), repassword.get())
                .compose(WanNetwork.applyService(getLifecycleProvider(), new WanObserver<SameBean<UserBean>>() {
                    @Override
                    public void onWanSuccess(SameBean<UserBean> userBeanWanBean) {
                        UserBean userBean = userBeanWanBean.getData();
                        KLog.e(userBean.toString());

                        SPUtils.getInstance().put("userId", userBean.toString());

                        // 记住用户名和密码
                        SPUtils.getInstance().put("userName", Objects.requireNonNull(userName.get()));

                        ToastUtils.showShort("注册成功，请再次登录！");
                        //进入MainActivity页面
                        startActivity(LoginActivity.class);
                        //关闭页面
                        finish();
                    }

                    @Override
                    public void onWanFailure(String e) {
                        KLog.e(e);
                        ToastUtils.showShort(e);
                    }
                }));
    }

    private void login() {
        startActivity(LoginActivity.class);
        finish();
    }
}
